home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / tags18.zip / LOG.C < prev    next >
C/C++ Source or Header  |  1991-09-25  |  3KB  |  111 lines

  1. /*
  2.  EPSHeader
  3.  
  4.    File: log.c
  5.    Author: J. Kercheval
  6.    Created: Sat, 06/08/1991  09:24:14
  7. */
  8. /*
  9.  EPSRevision History
  10.  
  11.    J. Kercheval  Sat, 06/08/1991  09:24:27  move from fileio.c
  12.    J. Kercheval  Wed, 06/26/1991  21:30:57  use standard fileio
  13.    J. Kercheval  Fri, 07/12/1991  23:09:16  use stderr as default
  14. */
  15.  
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "log.h"
  20.  
  21.  
  22. /*============================================================================
  23.  
  24.                           LOG FILE ROUTINES
  25.  
  26. ============================================================================*/
  27.  
  28.  
  29. static BOOLEAN log_is_open = FALSE;     /* if true then file has been opened */
  30. static BOOLEAN use_stderr = TRUE;       /* if true log to stderr */
  31.  
  32. static FILE *log_file;          /* log_file is the output log file pointer */
  33.  
  34.  
  35. /*----------------------------------------------------------------------------
  36. *
  37. *  open the log file after closing any existing log file
  38. *
  39. ----------------------------------------------------------------------------*/
  40.  
  41. BOOLEAN log_open(char *fname, BOOLEAN quiet, BOOLEAN overwrite)
  42. {
  43.     char open_type[3];          /* used to determine the open file type */
  44.  
  45.     /* assign the file open type */
  46.     if (overwrite) {
  47.         strcpy(open_type, "w");
  48.     }
  49.     else {
  50.         strcpy(open_type, "a");
  51.     }
  52.  
  53.     /* close the log if already open */
  54.     if (log_is_open)
  55.         log_close();
  56.  
  57.     use_stderr = !quiet;
  58.  
  59.     /* open file if non-zero file name */
  60.     if (strlen(fname)) {
  61.         /* try to open the log file */
  62.         if ((log_file = fopen(fname, open_type)) == (FILE *) NULL) {
  63.             return FALSE;
  64.         }
  65.         else {
  66.  
  67.             /* ah, sweet success */
  68.             log_is_open = TRUE;
  69.             return TRUE;
  70.         }
  71.     }
  72.     else {
  73.  
  74.         /* no log file */
  75.         log_is_open = FALSE;
  76.         return TRUE;
  77.     }
  78. }
  79.  
  80.  
  81. /*----------------------------------------------------------------------------
  82. *
  83. *  close the log file, if open
  84. *
  85. ----------------------------------------------------------------------------*/
  86.  
  87. void log_close()
  88. {
  89.     if (log_is_open) {
  90.         fclose(log_file);
  91.         log_is_open = FALSE;
  92.     }
  93. }
  94.  
  95.  
  96. /*----------------------------------------------------------------------------
  97. *
  98. *  log message to stderr and log_file if open
  99. *
  100. ----------------------------------------------------------------------------*/
  101.  
  102. void log_message(char *message)
  103. {
  104.     if (log_is_open) {
  105.         fprintf(log_file, "%s\n", message);
  106.     }
  107.     if (use_stderr) {
  108.         fprintf(stderr, "%s\n", message);
  109.     }
  110. }
  111.